﻿Imports Utilitario
Imports Negocio
Public Class frmBusqDocumento

    Dim servicio1 As New PedidoNegocio
    Dim servicio2 As New ClienteNegocio
    Dim util As New Utiles

    Dim codCliente As String = ""
    Dim codDocumento As String = ""

    Sub New()

        ' Llamada necesaria para el diseñador.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub

    Sub New(cliente As String, tdoc As String)

        ' Llamada necesaria para el diseñador.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        Me.codCliente = cliente
        Me.codDocumento = tdoc
    End Sub

    Sub iniciar()
        If codCliente = "" Then
            txtCodCliente.ReadOnly = False
            txtTDOC.Text = codDocumento
            util.enfocar()
        Else
            txtCodCliente.Text = codCliente
            util.enfocar()
            txtCodCliente.ReadOnly = True
        End If
    End Sub

    Private Sub frmBusqDocumento_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        iniciar()
    End Sub

    Private Sub txtCodCliente_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtCodCliente.KeyDown
        Dim texto As String = txtCodCliente.Text
        If e.KeyValue = Keys.Enter Then
            If texto = "" Then
                lblNomCliente.Text = ""
            Else
                Dim ds As DataSet = servicio2.obtenerClientesXFiltro(texto, "", "", "", "0", "0", "0")
                If ds.Tables(0).Rows.Count > 0 Then
                    lblNomCliente.Text = ds.Tables(0).Rows(0)("CliDescripcion").ToString.Trim
                End If
            End If
            txtTDOC.Text = codDocumento
            txtTDOC.Focus()
            util.enfocar()
        ElseIf e.KeyValue = Keys.Add Then

        End If
    End Sub

    Private Sub txtTDOC_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtTDOC.KeyDown
        Dim texto As String = codDocumento
        If e.KeyValue = Keys.Enter Then
            Dim ds As DataSet = servicio1.obtenerTiposDocumentos(texto, 3)
            If ds.Tables(0).Rows.Count > 0 Then
                lblNomDocumento.Text = ds.Tables(0).Rows(0)("TDO_Descripcion").ToString.Trim
            End If
        End If
    End Sub

    Private Sub chkFecha_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkFecha.CheckedChanged
        If chkFecha.Checked = True Then
            pnlPanel.Visible = True
        Else
            pnlPanel.Visible = False
        End If
    End Sub

    Sub validarFechas(dtpFecha As DateTimePicker)
        If CDate(dtpFecha.Text) > CDate(Date.Now.ToString("dd/MM/yyyy")) Then
            util.mensaje("Fecha excede el dia de hoy.")
            dtpFecha.Text = Date.Now.ToString("dd/MM/yyyy")
        End If
    End Sub

    Private Sub dtpHasta_ValueChanged(sender As Object, e As System.EventArgs) Handles dtpHasta.ValueChanged
        validarFechas(dtpHasta)
    End Sub

    Private Sub dtpDesde_ValueChanged(sender As Object, e As System.EventArgs) Handles dtpDesde.ValueChanged
        validarFechas(dtpDesde)
    End Sub

    Sub procesarListado()
        dgvPedido.Rows.Clear()
        Dim fecha1 As String = IIf(chkFecha.Checked = True, dtpDesde.Text, "")
        Dim fecha2 As String = IIf(chkFecha.Checked = True, dtpHasta.Text, "")
        Dim ds As DataSet = servicio1.obtenerPedidosXFiltro("", txtCodCliente.Text, txtTDOC.Text, fecha1, fecha2, txtVendedor.Text)
        For i = 0 To ds.Tables(0).Rows.Count - 1
            dgvPedido.Rows.Add(ds.Tables(0).Rows(i)("DCI_Codigo").ToString.Trim, ds.Tables(0).Rows(i)("Fecha").ToString.Trim,
                               ds.Tables(0).Rows(i)("CLI_Descripcion").ToString.Trim, ds.Tables(0).Rows(i)("VEN_DesCorta").ToString.Trim,
                               ds.Tables(0).Rows(i)("MON_Abreviatura").ToString.Trim, ds.Tables(0).Rows(i)("TDO_Abreviatura").ToString.Trim,
                               ds.Tables(0).Rows(i)("CNV_Descripcion").ToString.Trim, ds.Tables(0).Rows(i)("FOP_Descripcion").ToString.Trim, "")
        Next
    End Sub

    Private Sub btnProcesar_Click(sender As System.Object, e As System.EventArgs) Handles btnProcesar.Click
        procesarListado()
    End Sub

    Private Sub dgvPedido_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPedido.CellClick
        Dim f As Integer = e.RowIndex

        If f > -1 Then
            'frmPedido.txtCodPedido.Text = dgvPedido.Item("Column1", f).Value
            frmDocumento.txtPedido.Text = dgvPedido.Item("Column1", f).Value
            DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub dgvPedido_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dgvPedido.KeyDown
        If e.KeyValue = Keys.Enter Then
            frmDocumento.txtPedido.Text = dgvPedido.Rows(dgvPedido.CurrentRow.Index).Cells("Column1").Value
            DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

End Class